package club.banyuan.reserve.controller.api;

import club.banyuan.reserve.common.CommonResult;
import club.banyuan.reserve.dto.UserParam;
import club.banyuan.reserve.model.User;
import club.banyuan.reserve.service.AdminService;
import club.banyuan.reserve.service.OrderService;
import club.banyuan.reserve.vo.AdminOrderListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author HanChao
 * 描述信息：
 */
@RestController
@RequestMapping(value = "/api/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private OrderService orderService;

    @PostMapping(value = "/login")
    public CommonResult login (@RequestBody UserParam userParam,
                               HttpSession session) {

        User adminUser = adminService.adminUserLogin(userParam.getUsername(), userParam.getPassword());

        session.setAttribute("admin",adminUser);
        return CommonResult.success("OK");
    }

    @GetMapping(value = "/logout")
    public CommonResult logout (HttpSession session) {

        adminService.logout(session, "admin");
        return CommonResult.success("OK");
    }

    @GetMapping(value = "/current")
    public CommonResult current (HttpSession session) {

        if (session.getAttribute("admin") == null) {
            return CommonResult.failed("null");
        }

        return CommonResult.success((User) session.getAttribute("admin"));
    }

    @GetMapping(value = "/showorder")
    public CommonResult showOrder () {

        List<AdminOrderListVo> ordersList = orderService.showAdminOrderList();
        return CommonResult.success(ordersList);
    }
}
